# -*- coding: utf-8 -*-
from __future__ import division, unicode_literals

from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.db.models import IntegerField, CharField
from wenlincms.boot import admin_site


class CrossForeignIntegerField(IntegerField):
    """
    可跨数据库的外键，admin后台控件支持
    非池，2016-07
    """

    def __init__(self, *args, **kwargs):
        self.null = True
        self.blank = True
        super(CrossForeignIntegerField, self).__init__(*args, **kwargs)

    def formfield(self, **kwargs):
        from wlmodels.codes.models import CrossForeignKeys
        kwargs["widget"] = ForeignKeyRawIdWidget(CrossForeignKeys._meta.get_field(self.db_column).rel, admin_site)
        return super(CrossForeignIntegerField, self).formfield(**kwargs)


class CrossForeignCharField(CharField):
    """
    可跨数据库的外键，admin后台控件支持
    非池，2016-07
    """

    def __init__(self, *args, **kwargs):
        self.null = True
        self.blank = True
        super(CrossForeignCharField, self).__init__(*args, **kwargs)

    def formfield(self, **kwargs):
        from wlmodels.codes.models import CrossForeignKeys
        kwargs["widget"] = ForeignKeyRawIdWidget(CrossForeignKeys._meta.get_field(self.db_column).rel, admin_site)
        return super(CrossForeignCharField, self).formfield(**kwargs)
